package org.peterbjornx.landscape;

import org.peterbjornx.io.RSInputStream;
import org.peterbjornx.io.AbstractFileStore;

import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: PeterbjornX
 * Date: Apr 9, 2009
 * Time: 8:21:19 PM
 * Defines a in-memory RuneScape format landscape
 */
public class Landscape {
    public static final int MAP_WIDTH = 256;
    public static final int MAP_HEIGHT = 256;
    private static final int MAP1_WIDTH = 64;
    private static final int MAP1_HEIGHT = 64;
    public static final int HEIGHT_COUNT = 4;
    private static final int MAP_C = 4; // 4*4 Maps
    public int[][][] heightmap = new int[HEIGHT_COUNT][MAP_WIDTH][MAP_HEIGHT];
    public int[][][] overlay = new int[HEIGHT_COUNT][MAP_WIDTH][MAP_HEIGHT];
    public int[][][] underlay = new int[HEIGHT_COUNT][MAP_WIDTH][MAP_HEIGHT];
    private int[][][] shape = new int[HEIGHT_COUNT][MAP_WIDTH][MAP_HEIGHT];
    private int[][][] settings = new int[HEIGHT_COUNT][MAP_WIDTH][MAP_HEIGHT];
    private static final int MAIN_STORY = 0;

    public void loadMultiMap(int basex,int basey, AbstractFileStore f) throws IOException {
        int mb = MAP_C / 2;
        for (int mx = basex - mb;mx < ( basex + mb );mx++)
            for (int my = basex - mb;my < ( basey + mb );my++){
                if (LandscapeIndex.getMapIndex(false,mx,my) == -1)
                    continue;
                RSInputStream s = f.getFile(LandscapeIndex.getMapIndex(false,mx,my)+".dat");
                for (int gZ = 0; gZ < HEIGHT_COUNT;gZ++)
                    for (int x = 0; x < MAP1_WIDTH;x++)
                        for (int y = 0; y < MAP1_HEIGHT;y++)
                            readTile(x+((mx-basex)+mb)*64,y+((my-basey)+mb)*64,gZ,s,mx,my);
            }
    }

    @SuppressWarnings({"ResultOfMethodCallIgnored"})
    private void readTile(int x, int y ,int gZ, RSInputStream s,int mx,int my) throws IOException {
         if (x >= 0 &&  y >= 0 && x < MAP_WIDTH && y < MAP_HEIGHT){
             //System.out.println("MapOP:"+x+" "+y);
             do {
                int opcode = s.read();
                if (opcode == 0){
                    if (gZ == MAIN_STORY)
                        heightmap[gZ][x][y] = HeightGen.generateHeight(0xe3b7b + x + mx, 0x87cce + y + my);
                    else
                        heightmap[gZ][x][y] = heightmap[gZ-1][x][y] + 30;

                    break;
                }
                else if (opcode == 1)
                {
                    if (gZ == MAIN_STORY)
                        heightmap[gZ][x][y] = s.read();
                    else
                        heightmap[gZ][x][y] = heightmap[gZ-1][x][y] + s.read();
                    break;
                } else if (opcode <= 49)
                {
                    overlay[gZ][x][y] = s.read();
                    shape[gZ][x][y] = opcode - 2;
                } else if (opcode <= 81)
                    settings[gZ][x][y] = opcode - 49;
                else
                    underlay[gZ][x][y] = opcode - 81;

             } while (true);
         } else {
             System.err.println("MapOP:"+x+" "+y);
             do {
                int opcode = s.read();
                 if (opcode == 0)
                     break;
                 else if (opcode == 1)
                 {
                     s.read();
                     break;
                 }
                 else if (opcode <= 49)
                     s.read();
             } while (true);
         }
     }

}
